﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;
using NHibernateSample.Domain.Entities;
using NHibernate;

namespace NHibernateSample.DAL.Test
{
    [TestFixture]
    public class RelationFixture
    {
        private Relation _relation;
        private NHibernateHelper _helper;

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            _helper = new NHibernateHelper();
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
        }

        [SetUp]
        public void Setup()
        {
            _relation = new Relation(_helper.GetSession());
        }

        [Test]
        public void ReturnFullNameTest()
        {
            IList<Customer> customers = _relation.ReturnFullName("四", "李");
            foreach (Customer c in customers)
            {
                Assert.AreEqual("四 李", c.Name.FullName);
            }
        }

        [Test]
        public void UseHQL_GetCustomersWithOrdersTest()
        {
            IList<Customer> customers = _relation.UseHQL_GetCustomersWithOrders(new DateTime(2008, 10, 1));
            foreach (Customer c in customers)
            {
                foreach (Order o in c.Orders)
                {
                    Assert.GreaterOrEqual(o.OrderDate, new DateTime(2008, 10, 1));
                }
            }
            foreach (Customer c in customers)
            {
                Assert.AreEqual(1, customers.Count<Customer>(x => x == c));
            }
        }

        [Test]
        public void UseCriteriaAPI_GetCustomerswithOrdersHavingProductTest()
        {
            IList<Customer> customers = _relation.UseCriteriaAPI_GetCustomerswithOrdersHavingProduct();
            bool found = false;
            foreach (Customer c in customers)
            {
                foreach (Order o in c.Orders)
                {
                    foreach (Product p in o.Products)
                    {
                        if (p.Name == "ProductName1")
                        {
                            found = true;
                            break;
                        }
                    }
                }
            }
            Assert.IsTrue(found);
        }
    }
}
